package edu.whut.controller;

import edu.whut.entity.CommonResult;
import edu.whut.entity.Creation;
import edu.whut.service.CreationService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/creation")
public class CreationController {

    @Resource
    CreationService creationService;

    @RequestMapping("/add")
    public CommonResult<Creation> addCreation(@RequestBody Creation creation){
        if(creationService.addCreation(creation)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/delete")
    public CommonResult<Creation> deleteCreation(@RequestParam("creationId") Long creationId){
        if(creationService.deleteCreation(creationId)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/update")
    public CommonResult<Creation> updateCreation(@RequestBody Creation creation){
        if(creationService.updateCreation(creation)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/get")
    public CommonResult<Creation> getCreation(@RequestParam("creationId") Long creationId){
        if(creationService.getCreation(creationId)!=null){
            return new CommonResult<>(200,"success", creationService.getCreation(creationId));
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/getByUsername")
    public CommonResult<List<Creation>> getCreationByUsername(@RequestParam("username") String username){
        if(creationService.getCreationByUsername(username)!=null){
            return new CommonResult<>(200,"success", creationService.getCreationByUsername(username));
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/getAll")
    public CommonResult<List<Creation>> getAllCreation(){
        if(creationService.getAllCreation()!=null){
            return new CommonResult<>(200,"success", creationService.getAllCreation());
        }
        return new CommonResult<>(400,"error",null);
    }
}
